﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using SgmlReaderDll;

namespace mVu
{
  internal class Tnaflix : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, "http://www.tnaflix.com"},
                           {"Amateur", "http://www.tnaflix.com/amateur-porn"},
                           {"Anal", "http://www.tnaflix.com/anal-porn"}
                         };

      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, "most-recent"},
                      {"Being Watched", "being-watched"},
                      {"Most Recent", "most-recent"},
                      {"HQ Movies", "hd-clip"},
                      {"Most Viewed", "most-viewed"},
                      {"Top Rated", "top-rated"},
                      {"Full Movies", "full-length"}
                    };

      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"MP4", "MP4"},
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      if (String.IsNullOrEmpty(baseAddress))
      {
        baseAddress = "http://www.tnaflix.com";
      }

      var address = !String.IsNullOrEmpty(sort) ? String.Format("{0}/{1}/{2}", baseAddress, sort, page) : String.Format("{0}/{1}/{2}", baseAddress, "most-recent", page);
      // Need to add code for search query
      var result = GetPageSource(address);

      if (!String.IsNullOrEmpty(result))
      {
        var rh = new SGMLReaderHelper();
        var xhtml = rh.ProcessString(result);

        var doc = XDocument.Parse(xhtml);

        list.AddRange(from node in doc.Descendants("a")
                      where node.Attribute("class") != null && node.Attribute("class").Value.Equals("videoThumb")
                      select new Thumbnail
                      {
                        Image = node.Descendants("img").First().Attribute("src").Value,
                        Url = node.Attribute("href").Value,
                        Title = node.Attribute("title").Value,
                        Length = String.Empty
                      });
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      if (!String.IsNullOrEmpty(url))
      {
        var result = GetPageSource(url);

        if (!String.IsNullOrEmpty(result))
        {
          var rh = new SGMLReaderHelper();
          var xhtml = rh.ProcessString(result);

          var doc = XDocument.Parse(xhtml);

          var flv = from node in doc.Descendants("a")
                    where node.Attribute("class") != null && node.Attribute("class").Value.Equals("downloadButton")
                    && node.Value != null && node.Value.Contains("FLV")
                    select new
                    {
                      Url = node.Attribute("href").Value
                    };

          var mp4 = from node in doc.Descendants("a")
                    where node.Attribute("class") != null && node.Attribute("class").Value.Equals("downloadButton") 
                    && node.Value != null && node.Value.Contains("MP4")
                    select new
                    {
                      Url = node.Attribute("href").Value
                    };

          dictionary.Add("FLV", flv.First().Url);
          dictionary.Add("MP4", mp4.First().Url);
        }
      }

      return dictionary;
    }
  }
}